// components/personal-info/personalInfo.js
const util = require('../../utils/util.js')
const { validate } = require('../../utils/validator.js')
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
      observer (info) {
        this.init()
      }
    },
    isFamily: {
      type: Boolean,
      value: false
    },
    professionList: { // 职业
      type: Array,
      value: [],
      observer (professionList, oldVal) {
        const { profession } = this.data.info
        professionList.unshift('')
        const professionIndex = professionList.indexOf(profession)
        this.setData({professionIndex, professionList})
      }
    },
    annualIncomeList: { // 年收入
      type: Array,
      value: [],
      observer (annualIncomeList, oldVal) {
        const {annualIncome } = this.data.info
        annualIncomeList.unshift({code: '', name: ''})
        const annualIncomeIndex = annualIncomeList.findIndex(({code}) => annualIncome === code)
        this.setData({annualIncomeIndex, annualIncomeList})
      }
    },
    rules: Object
  },

  options: {
    addGlobalClass: true
  },
  
  /**
   * 组件的初始数据
   */
  data: {
    sexList: [ '男', '女' ],
    sexIndex: -1,
    endDate: (util.timestampToTime(Date.now() - 0 * 60 * 60 * 24 * 1000)),
    startDate: '1900-01-01',
    relationList: [
      {name: '配偶', code: 'couple'},
      {name: '父母', code: 'parent '},
      {name: '子女', code: 'child'},
    ],
    relationIndex: -1,
    professionIndex: -1,
    annualIncomeIndex: -1,
  },

  attached () {
    this.init()
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init () {
      const { sexList, professionList, annualIncomeList, info, relationList } = this.data
      const { sex, annualIncome, profession, relation } = info
      const relationIndex = relationList.findIndex(({code}) => code === relation)
      const sexIndex = sexList.indexOf(sex)
      const professionIndex = professionList.indexOf(profession)
      const annualIncomeIndex = annualIncomeList.findIndex(({code}) => annualIncome === code)
      this.setData({
        relationIndex,
        sexIndex,
        annualIncomeIndex,
        professionIndex
      })
    },
    bindDateChange: function(e) {
      this.setData({
        'info.birthday': e.detail.value
      })
      this.triggerEvent('change', { info: this.data.info }, {});
    },
    bindRelationChange: function (e) {
      const relationIdx = e.detail.value
      this.setData({
        relationIdx
      })
      this.setData({
        'info.relation': this.data.relationList[relationIdx].label
      })
      this.triggerEvent('change', { info: this.data.info }, {});
    },
    inputInfo: function (e) {
      const { key } = e.currentTarget.dataset;
      const value = e.detail.value;
      this.setData({
        [`info.${key}`]: value
      })
      validate(key, this.data.rules[key], value)
      this.triggerEvent('change', { info: this.data.info }, {});
    },
    bindPickerChange: function (e) {
      const index = e.detail.value
      const { key, valueKey } = e.currentTarget.dataset;
      const value = valueKey ? this.data[key + 'List'][index][valueKey] : this.data[key + 'List'][index]
      this.setData({
        [key + 'Index']: index,
        [`info.${key}`]: value
      })
      this.triggerEvent('change', { info: this.data.info }, {})
    },
    blurIdNumber (e) {
      const { key } = e.currentTarget.dataset;
      const { idType } = this.data.info
      const value = e.detail.value;
      this.setData({
        [`info.${key}`]: value
      })
      validate(key, this.data.rules[key], value, (valid) => {
        if (valid) {
          if (idType === '0') {
            if (/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(value)) {
              let birthday = value.slice(6, 14)
              const y = birthday.slice(0, 4)
              const m = birthday.slice(4, 6)
              const d = birthday.slice(6, 8)
              birthday = `${y}-${m}-${d}`
              const sex = parseInt(value.slice(-2, -1)) % 2 == 1 ? '男' : '女'
              this.setData({
                [`info.birthday`]: birthday,
                [`info.sex`]: sex,
              })
              this.triggerEvent('change', { info: this.data.info }, {});
            } else {
              if (value) {
                wx.showToast({
                  title: '身份证号填写错误',
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          }
        }
      })
      this.triggerEvent('change', { info: this.data.info }, {});
    },
    onTouchStart () {
      // fix键盘展开后按住页面不放滚动，输入框的内容会跟着 滚动
      wx.hideKeyboard()
    }
  }
})
